package com.atguigu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.entity.product.Category;
import com.atguigu.exception.ResultException;
import com.atguigu.mapper.CategoryMapper;
import com.atguigu.service.CategoryService;
import com.atguigu.utils.ExcelListener;
import com.atguigu.vo.common.ResultCodeEnum;
import com.atguigu.vo.product.CategoryExcelVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findListByParentId(Long parentId) {
        List<Category> categoryList = categoryMapper.findListByParentId(parentId);
        categoryList.forEach(category -> {
            Long categoryId = category.getId();
            int count = categoryMapper.selectCountByParentId(categoryId);
            if (count == 0) {
                category.setHasChildren(false);
            } else {
                category.setHasChildren(true);
            }
        });
        return categoryList;
    }

    @Override
    public void download(HttpServletResponse response) {
        try {
            //1 设置响应头信息和其他信息
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");

            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("分类数据", "UTF-8");

            //设置响应头信息 Content-disposition
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            //2 调用mapper方法查询所有分类，返回list集合
            List<Category> categoryList = categoryMapper.findAll();

            List<CategoryExcelVo> categoryExcelVoList = new ArrayList<>();
            for (Category category : categoryList) {
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                BeanUtils.copyProperties(category, categoryExcelVo);
                categoryExcelVoList.add(categoryExcelVo);
            }

            EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class).sheet("分类数据")
                    .doWrite(categoryExcelVoList);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    @Override
    public void upload(MultipartFile file) {
        try {
            ExcelListener<CategoryExcelVo> excelListener = new ExcelListener(categoryMapper);
            EasyExcel.read(file.getInputStream(), CategoryExcelVo.class, excelListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ResultException(ResultCodeEnum.DATA_ERROR);
        }
    }
}
